﻿require.config({
    paths: {
        Msg: "Scripts/Message/js/Message"
    }
});
require(["Msg"], function (MsgClass) {
    var tip;
    var ChatConn = new MsgClass({ URL: "Message/ChatHandler.ashx", Delay: 3000 });

    window.ChatStore = [];
    window.ChatSubscibeEvents = {}; //{订阅ID:{func:回调,context:window}}

    $(function () {
        //根据配置文件控制站内信的显示
        $("<li id='chatLetterLi'>")
            .html('<a class="chat_tt_m" href="#" title="通讯">通讯</a><p id="chatCountor"></p>')
            .click(function () {
                top.addTabExtend(this, 'Message/ChatPage.aspx', '通讯');
                $("#chatCountor").empty();
            }).insertBefore($("#line-li"));

        //接收信息
        ChatConn.receive(function (msg) {
            window.ChatStore = window.ChatStore.concat(msg);
            $("#chatCountor").html(msg.length || "");
            var content = $("<a href='###'>您有新的消息，请注意查收。</a>")
                .click(function () {
                    top.addTabExtend($('#chatLetterLi'), 'Message/ChatPage.aspx', '通讯');
                    $("#chatCountor").empty();
                    tip.hide();
                });
            if (!tip) {
                tip = $.ligerDialog.tip({ title: '提示信息', content: content, allowClose: false });
            } else {
                tip.set("content", content);
                tip.show();
            }
            //消息分发
            var events = window.ChatSubscibeEvents;
            var listener;
            for (var e in events) {
                listener = events[e];
                if (listener.context.location.href) {
                    setTimeout(function () { $("#chatCountor").empty() }, 2000);
                    listener.func.call(listener.context, msg);
                }
            }
        });

        ChatConn.run();
        window.ChatConn = ChatConn;
    });

    if (!window.Users) {
        $.get("Message/MsgPushHandler.ashx", { Func: "GetUsers" }, function (data) {
            if (!data) {
                $.ligerDialog.error("站内信用户列表获取失败！");
            }
            window.Users = data;
        }, "json");
    }
});